/*    
    This file is part of AWC2.

    AWC2 is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AWC2 is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with AWC2.  If not, see <http://www.gnu.org/licenses/>.
*/

var list;

function add(input) {
    var id = input.getAttribute("name");
    chrome.extension.sendMessage({action: "add", dominio: list[id] }, function(response) {});
    console.log("Richiesta di Blocco inviata! "+list[id]);
    window.close();
}


function remove(input) {
    var id = input.getAttribute("name");
    chrome.extension.sendMessage({action: "remove", dominio: list[id] }, function(response) {});
    console.log("Richiesta di Sblocco inviata! "+list[id]);
    window.close();
}

function in_array(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) {
            return true;
        }
    }
    return false;
}


function init() {


    list = localStorage.blacklist;
    list = (list ? list.split(",") : []);
    
    rate = localStorage.rate;
    rate = (rate ? rate.split(",") : []);
    
    var unblock = localStorage.unblocked;
    unblock = (unblock ? unblock.split(",") : []);
    
    console.log("Sbloccati: "+unblock);
    console.log("Bloccati: "+localStorage.blacklist);
    
    
    var content = document.getElementById('content');
    
    for(var i = 0; i < list.length; i++) {
        var div = document.createElement("DIV");
        var url2 = list[i];
        //url2 = url2.substr(1);
        //url2 = url2.slice(0, -1);
        console.log("|"+list[i]+"|");
        var input = document.createElement("INPUT");
        input.setAttribute("type","button");
        if(in_array(url2, unblock)) {
            // Unblocked
            input.setAttribute("name",i);
            input.setAttribute("value","Blocca");
        }
        else {
            input.setAttribute("name",i);
            input.setAttribute("value","Sblocca");
        }
        var color = "green";
        if(rate[i] <= 5.0) color = "red";
        
        var p = document.createElement("span");
        p.style.color = color;
        p.style.marginLeft = '10px';
        
        var testo = document.createTextNode(list[i] + " (" + rate[i] +")" );
        
        
        
        if(in_array(url2, unblock)) {
            console.log("Il dominio "+url2+" è sbloccato "+list[i]);
            input.addEventListener("click", function() { add(this) }, false);
        }
        else {
            console.log("Il dominio "+url2+" è bloccato "+list[i]);
            input.addEventListener("click", function() { remove(this) }, false);
        }
        
        p.appendChild(testo);
        div.appendChild(input);
        div.appendChild(p);
        content.appendChild(div);
    }
}


document.addEventListener("DOMContentLoaded", init, false);
